'****************************************************************************************
'QTP TFS GENERIC TEST BY ANNA RUSSO
'****************************************************************************************
strQTPTestPath = "C:\Program Files\HP\QuickTest Professional\Tests\Test1"
strQTPResultsFolder = "C:\QTP_TFSGenericTest\QTPResults"
strQTPResultsFile = "C:\QTP_TFSGenericTest\QTPResults\Report\Results.xml"
strXMLTransformFile = "C:\QTP_TFSGenericTest\QTP11_TFS_Transform.xsl"
strGenericTestFile = "C:\QTP_TFSGenericTest\TFSGenericTestResults\TFSGenericTestResult.xml"

Dim qtpApp
Dim qtpTest  
Dim qtpResults
Dim xmlTestResultSource
Dim xmlTransform
Dim strError
Dim strResult
Dim objFSO, file
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set xmlTestResultSource = CreateObject("Microsoft.XMLDOM")
Set xmlTransform = CreateObject("Microsoft.XMLDOM")
strOldText = "UTF-16"
strNewText = "UTF-8"
xmlTestResultSource.validateOnParse = True
xmlTransform.validateOnParse = True
xmlTestResultSource.async = False
xmlTransform.async = False

'****************************************************************************************
'SET QTP APP LAUNCH
'****************************************************************************************
Set qtpApp = CreateObject("QuickTest.Application") 
qtpApp.Launch 
qtpApp.Visible = True 

'****************************************************************************************
'OPEN QTP SCRIPT
'****************************************************************************************
qtpApp.Open strQTPTestPath, True
Set qtpTest = qtpApp.Test 

'****************************************************************************************
'SET THE QTP RESULTS LOCATION
'****************************************************************************************
Set qtpResults = CreateObject("QuickTest.RunResultsOptions") 
qtpResults.ResultsLocation = strQTPResultsFolder

'****************************************************************************************
'EXECUTE QTP TESTS
'****************************************************************************************
qtpTest.Run qtpResults, True 

'****************************************************************************************
'CLOSE AND QUIT QTP
'****************************************************************************************
qtpTest.Close 
qtpApp.Quit

'****************************************************************************************
'LOAD QTP RESULTS FILE AND TRANSFORM FILE
'****************************************************************************************
xmlTestResultSource.Load strQTPResultsFile
xmlTransform.Load strXMLTransformFile
strResult = xmlTestResultSource.transformNode(xmlTransform)

'****************************************************************************************
'WRITE QTP RESULTS IN TFS GENERIC TEST RESULT XML FORMAT
'****************************************************************************************
Set file = objFSO.OpenTextFile(strGenericTestFile, ForWriting, True)
file.write strResult
file.Close

'****************************************************************************************
'REPLACE UTF-16 WITH UTF-8
'****************************************************************************************
Set file = objFSO.OpenTextFile(strGenericTestFile, ForReading)
strText = file.ReadAll
file.Close
strNewText = Replace(strText, strOldText, strNewText)
Set file = objFSO.OpenTextFile(strGenericTestFile, ForWriting)
file.WriteLine strNewText
file.Close